
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Country
{
    public:
        Country();
        void buy(int pMoney);
        void sell(int pMoney);
        void setId(int id);
        void show();
        int id;
        int money;
        int times;

};
Country::Country()
{
    money = 0;
}
void Country::buy(int pMoney)
{
     money = money - pMoney;
     times = times + 1;
}

void Country::sell(int pMoney)
{
     money = money + pMoney;
     times = times + 1;
}
void Country::show()
{
    cout<<id<<" "<<money<<endl;
}
void Country::setId(int pid)
{
    id = pid;
}
int main()
{
   int N;
   cin >> N;
   vector<Country> all;
   for(int i = 0; i<=N; i++)
   {
        Country a;
        a.setId(i);
        all.push_back(a);
   }
   for(int i = 1; i<=N; i++)
   {
       int num;
       cin>>num;
       for(int j = 0; j<num; j++)
       {
           int cid,money;
           cin>>cid>>money;
           all[i].sell(money);
           all[cid].buy(money);
       }
   }
   sort(begin(all)+1,end(all),[](Country&a,Country& b){
       if(a.money != b.money)
            return a.money > b.money;
        else
        {
            if(a.times != b.times)
                return a.times>b.times;
            else
                return a.id < b.id;
        }
   });
   for(int i = 1; i<=N; i++)
         all[i].show();

}